import sys
import time

from libs.action import WebDriverAction
from libs.game import Game
from libs.image import ImageTool
from libs.log import Log
from libs.movement import Movement
from libs.task_helper import TaskHelper
from libs.trade_helper import TradeHelper
from libs.window import AdsWindow

if __name__ == "__main__":
    parser = TaskHelper.default_args_parser('Pixmax Cookie')
    args = parser.parse_args()
    task_helper = TaskHelper.from_args(args)

    user_id = args.user_id
    log = Log(user_id)


    try:
        driver = AdsWindow(user_id).open(False)
        game = Game(driver, user_id)
        action = WebDriverAction(driver)
        movement = Movement(driver)
        image_tool = ImageTool(driver)
        trade_helper = TradeHelper(driver, user_id)

        game.enter_game()
        door_loc = None
        for _ in range(3):
            door_loc = image_tool.find_target('carnival/carnival_door.png')
            if door_loc:
                break
            action.goto_spec()
            action.goto_terra()
            movement.keep_move_left(1)
            tent_loc = image_tool.find_target('carnival/carnival_tent.png')
            if tent_loc:
                action.move_to('carnival/carnival_tent.png', max_loop=3)
                action.move_by_offset(200, 200)
                tent_loc = image_tool.find_target('carnival/carnival_tent.png')
                action.click_by_offset(*tent_loc)
                action.wait_page_loaded()

        if door_loc is None:
            log.debug('enter carnival failed')
            task_helper.add_task('pixmas_cookie.py', delay_seconds=0, ignore_running=True, priority=9, retry=True)
            sys.exit()

        tree_loc = None
        for _ in range(3):
            tree_loc = image_tool.find_target('carnival/carnival_tree.png')
            if tree_loc:
                break
            movement.keep_move_up(5)

        tree_x, tree_y = tree_loc
        action.click_by_offset(tree_x + 45, tree_y + 120)
        time.sleep(2)
        action.handle_dialog()
        action.handle_dialog()

        movement.keep_move_left(0.5)
        movement.keep_move_up(5)

        doug_loc = image_tool.find_target('carnival/carnival_doug.png')
        doug_x, doug_y = doug_loc
        action.click_by_offset(doug_x, doug_y)
        time.sleep(2)
        action.handle_dialog()
        action.handle_dialog()

        movement.keep_move_right(0.5)
        movement.keep_move_up(3)
        movement.keep_move_down(0.3)
        movement.keep_move_left(1.5)
        movement.keep_move_up(3)


        action.handle_dialog()
        cookie_loc = image_tool.find_target('carnival/carnival_cookie.png')
        if cookie_loc:
            action.move_to('carnival/carnival_cookie.png', max_loop=3)

        action.move_by_offset(200, 200)
        cookie_loc = image_tool.find_target('carnival/carnival_cookie.png')
        cookie_x, cookie_y = cookie_loc
        action.click_by_offset(cookie_x, cookie_y)

        # # 买cookie
        # action.click_by_offset(cookie_x, cookie_y + 205)
        # time.sleep(1)
        # basket_name = '25 Cookie Credit Basket'
        # action.find_and_click_by_xpath(f"//div[text()='{basket_name}']/parent::div/parent::div", basket_name, 1)
        # time.sleep(1)
        # action.find_and_click_by_class('Store_buy-btn__gLsMk', 'BuyButton', 1)
        # log.debug(f'buy {basket_name}')
        # action.escape()
        # action.escape()
        # game.open_backpack()
        # basket_loc = image_tool.find_target('carnival_cookie_basket_25.png')
        # if basket_loc:
        #     basket_x, basket_y = basket_loc
        #     basket_x += 10
        #     basket_y += 10
        #     action.click_by_offset(basket_x, basket_y)
        #     action.click_by_offset(basket_x, basket_y)
        #     action.click_by_offset(basket_x, basket_y)
        #     game.close_backpack()
        #     time.sleep(1)
        #     action.click_by_offset(435, 80)
        #     time.sleep(1)
        #     game.get_backpack()

        # 放树
        game.get_backpack()
        tree_index, _ = game.get_backpack_index_and_qty("Pixmas Tree ('24)")
        if tree_index:
            action.goto_task_board()
            game.use_backpack_item(tree_index)
            action.move_by_offset(685, 150)
            time.sleep(1)
            action.click()
            time.sleep(1)
            action.move_by_offset(700, 500)
            time.sleep(1)
            action.click()
            action.escape()
            action.escape()

        task_helper.task_success()
        if game.get_backpack_qty('Cookie Credit') >= 10:
            task_helper.add_task('pixmas_guess.py', delay_seconds=0, ignore_running=True, priority=9)
        task_helper.add_task('pixmas_cookie.py', delay_seconds=60 * 60 * 24, ignore_running=True, priority=9)
    except Exception as e:
        task_helper.task_fail()
        task_helper.add_task('pixmas_cookie.py', delay_seconds=1 * 60, ignore_running=True, priority=9, retry=True)
        raise e